package com.yonyou.rabbitmq.controller;


import com.yonyou.rabbitmq.common.MessageConstants;
import com.yonyou.rabbitmq.common.enums.SiteEnum;
import com.yonyou.rabbitmq.producer.ExtCorrelationData;
import com.yonyou.rabbitmq.service.BaseProducerService;
import com.yonyou.rabbitmq.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class SendMessageController {

    @Autowired
    BaseProducerService baseProducerService;

    @Autowired
    MessageService messageService;

    @GetMapping("/testmq")
    public void testmq(){
        String message = "zw测试sendThreadManualWork模型";
        ExtCorrelationData ext = new ExtCorrelationData();
        baseProducerService.sendMsgStr("","threadWorkerQueue",message,ext, SiteEnum.ZW.getKey());
    }

    @GetMapping("/sendDelay")
    public void sendDelay(){
        for (int i = 0; i < 1; i++) {
            String message = "zw测试sendThreadManualWork模型";
            String message1 = "gf测试sendThreadManualWork模型";
            baseProducerService.sendDelayStr("pmfeaturesyncdev",message,10000,null, SiteEnum.ZW.getKey());
            baseProducerService.sendDelayStr("pmfeaturesyncdev",message1,5000,null, SiteEnum.GF.getKey());
        }
    }

//    @Autowired
//    DelayedProvider delayedProvider;

//    @GetMapping("/sendDirect")
//    public String sendDirect() {
//        String messageId = String.valueOf(UUID.randomUUID());
//        String messageData = "test message, hello!";
//        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//        Map<String,Object> map=new HashMap<>();
//        map.put("messageId",messageId);
//        map.put("messageData",messageData);
//        map.put("createTime",createTime);
//        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
//        try{
//            rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
//        }catch (AmqpException e){
//            log.error("发送消息失败，失败原因：{}",e);
//        }
//        return "ok";
//    }
//
//    @GetMapping("/sendDelay")
//    public String sendDelay(){
//        delayedProvider.send("five delayed-message",5000);
//        delayedProvider.send("three delayed-message",3000);
//        return "nice";
//    }
//
//    //worker模型(轮训，平均分配)
//    @GetMapping("/sendWork")
//    public String sendWork(){
//        for (int i = 0; i < 10; i++) {
//            String message = "测试work模型" + i;
//            rabbitTemplate.convertAndSend("workerQueue", message);
//        }
//        return "work模型";
//    }
//
//    //worker模型(能者多劳)
//    @GetMapping("/sendManualWork")
//    public String sendManualWork(){
//        String message = "测试manualWork模型";
//        for (int i = 0; i < 100; i++) {
//            rabbitTemplate.convertAndSend("manualWorkerQueue", message + i);
//        }
//        return "manualWork模型";
//    }

    //worker模型
//    @GetMapping("/sendThreadManualWork")
//    public String sendThreadManualWork(){
//        for (int i = 0; i < 1; i++) {
//            String message = "测试sendThreadManualWork模型" + i;
//            Message msg = MessageBuilder.withBody(message.getBytes()).build();
//            ExtCorrelationData ext = new ExtCorrelationData();
//            rabbitTemplate.send("","threadWorkerQueue",msg,ext);
//            gfrabbitTemplate.send("","threadWorkerQueue",msg,ext);
//        }
//        return "sendThreadManualWork模型";
//    }

    //查询队列中剩余消息数量
    @GetMapping("/getMessageCount")
    public int getMessageCount(String queueName){
        final int messageCount = messageService.getMessageCount(SiteEnum.GF.getKey(), queueName);
        return messageCount;
    }

}
